package com.zhongkai.web.control.config;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.logging.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhongkai.model.config.Action;
import com.zhongkai.service.config.ActionService;
import com.zhongkai.tools.CheckWord;

@Controller
public class ActionControl {

	private Logger log=Logger.getLogger(this.getClass());
	
	private ActionService actionService;

	@Resource
	public void setActionService(ActionService actionService) {
		this.actionService = actionService;
	}
	
	@RequestMapping(value="/system/config/role_manage_action.do")
	public String execute(HttpServletRequest request,HttpServletResponse response) throws Exception {
		PrintWriter out=response.getWriter();
		try {
			String rid=request.getParameter("roleId");
			if(rid==null || !rid.matches("^[0-9]*[1-9][0-9]*$")){
				out.write(CheckWord.showMsg("非法的参数！", "/system/desktop.jsp",request));
				return null;
			}
			Integer roleId=Integer.valueOf(rid);
			String[] actionKeys=request.getParameterValues("actionKey");
			if(actionKeys==null || actionKeys.length==0){
				actionService.deleteByRoleId(roleId);
			}
			else{
				List actionKeyList=actionService.findByHql("select actionKey from Action where roleId=?", roleId);
				if(actionKeyList!=null && actionKeyList.size()>0){
					StringBuffer deleteHql=null;
					StringBuffer deleteActionKey=null;
					String[] actionKeySort=actionKeys;
					Arrays.sort(actionKeySort);
					for(int i=0;i<actionKeyList.size();i++){
						String actionKey=actionKeyList.get(i).toString();
						if(Arrays.binarySearch(actionKeySort, actionKey)<0){
							if(deleteHql==null)
								deleteHql=new StringBuffer("delete from Action where roleId=").append(roleId).append(" and (");
							if(deleteActionKey==null)
								deleteActionKey=new StringBuffer(" actionKey='").append(actionKey).append("'");
							else
								deleteActionKey.append(" or actionKey='").append(actionKey).append("'");
						}
					}
					if(deleteHql!=null){
						deleteHql.append(deleteActionKey).append(")");
						actionService.deleteByHql(deleteHql.toString());
					}
					
					List actionList=null;
					Object[] actionKeyArray=actionKeyList.toArray();
					Arrays.sort(actionKeyArray);
					for(int i=0;i<actionKeys.length;i++){
						String actionKey=actionKeys[i];
						if(Arrays.binarySearch(actionKeyArray, actionKey)<0){
							if(actionList==null)
								actionList=new ArrayList();
							Action action=new Action();
							action.setActionId(null);
							String actionType=request.getParameter(actionKey);
							action.setActionType(Integer.valueOf(actionType));
							action.setRoleId(roleId);
							action.setActionKey(actionKeys[i]);
							actionList.add(action);
						}
					}
					if(actionList!=null)
						actionService.saveOrUpdateList(actionList);
				}
				else{
					List actionList=new ArrayList();
					for(int i=0;i<actionKeys.length;i++){
						Action action=new Action();
						action.setActionId(null);
						String actionType=request.getParameter(actionKeys[i]);
						action.setActionType(Integer.valueOf(actionType));
						action.setRoleId(roleId);
						action.setActionKey(actionKeys[i]);
						actionList.add(action);
					}
					actionService.saveOrUpdateList(actionList);
				}
			}
			out.write(CheckWord.showMsg("角色权限设置成功！", "/system/config/role_manage_list.jsp",request));
			return null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error("错误！角色权限设置时发生异常："+e.getMessage());
			out.write(CheckWord.showMsg("角色权限设置时发生异常，请与管理员联系!", "/system/config/role_manage_list.jsp",request));
			return null;
		}
	}
}
